package com.weixiaodong.ModelDesign.生成实例.builder模式.案例;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 定义HTML文件编辑器
 */
@SuppressWarnings("all")
public class HTMLBuilder extends Builder {

    private String fileName;
    private PrintWriter writer;
    private Document document;

    public HTMLBuilder(Document document) {
        this.document = document;
    }

    @Override
    public void makeTitle(String title) {
        fileName = title + ".html";
        try {
            writer = new PrintWriter(new FileWriter(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        writer.println("<html><head><title>" + title + "</title></head><body>");
        writer.println("<h1>" + title + "</h1>");
    }

    @Override
    public void makeString(String content) {
        writer.println("<p>" + content + "</p>");
    }

    @Override
    public void makeItems(String[] items) {
        writer.println("<ul>");
        for (String item : items) {
            writer.println("<li>" + item + "</li>");
        }
        writer.println("</ul>");
    }

    @Override
    public void close() {
        writer.println("</body></html>");
    }

    public String getFileName() {
        return fileName;
    }
}
